-- UIGDCloseActivity
-- Created by chengb May/7/2016
-- GD关闭活动

UIGDCloseActivity = class("UIGDCloseActivity", function()
    return cc.Layer:create();
end);

function UIGDCloseActivity.create()
    return UIGDCloseActivity.new();
end

-- 输入框最大数量
local MAX_INPUT_NUM = 4;

-- 待输入的字段
local fieldMemoList = {};

-- 构造函数
function UIGDCloseActivity:ctor()
    self:setName("UIGDCloseActivity");
    local node = cc.CSLoader:createNode("layout/GD/ContentEdit.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 100000);

    fieldMemoList = {
        getLocStr("activity_id_edit"),
    };

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIGDCloseActivity:registerTouchEvent()
    -- 确定
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:tryCloseActivity();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    btnOk:setTitleText(getLocStr("btn_text_close"));
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 返回
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIGDCloseActivity");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end


-- 尝试关闭活动
function UIGDCloseActivity:tryCloseActivity()
    local id = self.editList[1]:getText();

    -- 1.检查活动编号
    if string.len(id) == 0 then
        alert(getLocStr("please_input_activity_id"));
        return;
    end

    id = tonumber(id);
    if not ActivityM.query(id) then
        -- 活动编号错误
        alert(getLocStr("activity_id_error"));
        return;
    end

    -- 关闭活动
    GDM.closeActivity(id);
end

-- 重绘
function UIGDCloseActivity:redraw()
    local fieldNum = #fieldMemoList;
    self.editList = {};
    for i = 1, MAX_INPUT_NUM do
        local fieldNode = findChildByName(self.node, "CT/field" .. i);
        local nameLabel = findChildByName(fieldNode, "fieldname");
        local inputEdit = findChildByName(fieldNode, "field_input");

        if i > fieldNum then
            fieldNode:setVisible(false);
        else
            fieldNode:setVisible(true);

            local memo = fieldMemoList[i];
            TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL , TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true);
            nameLabel:setString(memo);

            -- 设置编辑框
            local editBoxSize1 = inputEdit:getContentSize();
            local editBox = ccui.EditBox:create(editBoxSize1, "images/ui/login/1.png");
            editBox:setAnchorPoint(cc.p(0, 0));
            editBox:setPosition(inputEdit:getPosition());
            editBox:setFontName(getLocFontName());
            editBox:setFontSize(TextStyleM.TEXT_SIZE_TINY);
            editBox:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_NORMAL);
            inputEdit:setPlaceHolder("");
            editBox:setText("");
            editBox:setMaxLength(16);
            editBox:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
            inputEdit:getParent():addChild(editBox);

            table.insert(self.editList, editBox);
        end
    end
end

-- 适配
function UIGDCloseActivity:resize()
    local node = self.node
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT底部居中并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end







